<?php
namespace Home\Controller;;
$upload->ext = array('csv');
;
$upload->ext = array('csv');

use Think\Controller;
class UploadController extends Controller {
    public function index(){
        $sql = "SELECT * FROM employee";
        $this->assign("users",D()->query($sql));
        $this->display("index");
    }


    /*
    public function uploadFile(){

        $file = __ROOT__.'Public/upload/';
        $upload=  new \Think\Upload();
        $upload->maxsize = 314555555;
        $upload->exts = array('csv');
        $upload->rootPath = $file;
        $upload->savePath = '';
        $upload->replace = true;
        $upload->saveName = uniqid();

        $info = $upload->upload();

        if(!$info){
            $this->error($upload->getError());
        }else{
            $path = $file.$info['name']['savepath'].$info['name']['savename'];

            $f = fopen($path,'r');

            while($array = fgetcsv($f,$delimiter=',')){
                $data[] = $array;
            }

            $conn = mysqli_connect('localhost','root','123456','data');

            array_shift($data);
            foreach ($data as $key) {
                $SQL = "INSERT INTO total(eid,date,count,greater1,greater3,yuefang) VALUES($key[0],'$key[1]',$key[3],$key[4],'$key[5]',$key[6])\n";

                mysqli_query($conn,$SQL);
                $this->success("添加成功",'/');

            }
        }


    }
     */
    public function add(){
        $eid = I('POST.eid');
        $count = I('POST.count');
        $date = I('POST.date');
        $greater1 = I('POST.greater1');
        $greater3 = I('POST.greater3');

        if($eid && $count && $greater1 && $greater3 &&$date){
            $data['eid'] = $eid;
            $data['date'] = $date;
            $data['count'] = $count;
            $data['greater1'] = $greater1;
            $data['greater3'] = $greater3;

            $date = date('Y-m-d');
            $SQL = "SELECT * from total where eid = '{$eid}' and date='{$date}'";
            if(D()->query($SQL)){
                $reponse['status'] = 0;
                $response['msg'] = "已经存在";
                die(json_encode($response));
            }

            $SQL = "INSERT INTO total(eid,date,count,greater1,greater3) values({$eid},'{$date}',{$count},{$greater1},{$greater3})";

            $data['sql'] = $SQL;

            $model = M("total");
            if($model->execute($SQL)){
                $response['status'] = 1;
                $response['msg'] = "添加成功";
                $response['date'] = $SQL;
                die(json_encode($response));
            }

        }else{
            $reponse['status'] = 0;
            $response['msg'] = "非法提交";
            die(json_encode($response));
        }
    }
    public function addother(){
        $eid = I('POST.eid');
        $date = I('POST.date');
        $yixiang = I('POST.yixiang');
        $qianyue = I('POST.qianyue');
        $yuefang = I('POST.yuefang');

        if($eid && $date && $yuefang && $qianyue &&$yixiang){
            $date = date('Y-m-d');
            $SQL = "SELECT * from other where eid = '{$eid}' and date='{$date}'";
            if(D()->query($SQL)){
                $reponse['status'] = 0;
                $response['msg'] = "已经存在";
                die(json_encode($response));
            }

            $SQL = "INSERT INTO other(eid,date,yuefang,yixiang,qianyue) values({$eid},'{$date}',{$yuefang},{$yixiang},{$qianyue})";

            $data['sql'] = $SQL;

            $model = M("total");
            if($model->execute($SQL)){
                $response['status'] = 1;
                $response['msg'] = "添加成功";
                $response['date'] = $SQL;
                die(json_encode($response));
            }

        }else{
            $reponse['status'] = 0;
            $response['msg'] = "非法提交";
            $response['date'] =$date;
            die(json_encode($response));
        }
    }

    public function test(){
        $date = date('Y-m-d');
        echo $date;
        $SQL = "SELECT * from total where eid = 1 and date='{$data}'";
        print $SQL;
    }

}
